#include "frame.h"
#include "IO.h"
#include "timer.h"

#define USART1               (&AVR32_USART1)
#define USART1_RX_PIN        AVR32_USART1_RXD_0_0_PIN
#define USART1_RX_FUNCTION   AVR32_USART1_RXD_0_0_FUNCTION
#define USART1_TX_PIN        AVR32_USART1_TXD_0_0_PIN
#define USART1_TX_FUNCTION   AVR32_USART1_TXD_0_0_FUNCTION

#define USART0               (&AVR32_USART0)
#define USART0_RX_PIN        AVR32_USART0_RXD_0_0_PIN
#define USART0_RX_FUNCTION   AVR32_USART0_RXD_0_0_FUNCTION
#define USART0_TX_PIN        AVR32_USART0_TXD_0_0_PIN
#define USART0_TX_FUNCTION   AVR32_USART0_TXD_0_0_FUNCTION

int start;
int direction;
int speed2;


void initUsartTimer(void){
	startLED =1;
	directionLED =0;
	speedLED =1;
	ledSpeed = 500;

	timerLedId = setTimer(blinkLed, ledSpeed, 0);


}

void checkTimerLed(void){
	int x =0;
	if(isTimerOn(timerLedId) == 1){
		x=1;
		stopTimer(timerLedId);
	}

	if(directionLED == 1){
		ledDirectionFlag = 'L';
	}else if (directionLED == 2){
		ledDirectionFlag = 'P';
	}

	if (speedLED ==  1){
		ledSpeed = 2*ledSpeed;
		changeTimer(timerLedId, ledSpeed);
	}else if(speedLED == 2){
		ledSpeed = 0.5*ledSpeed;
		changeTimer(timerLedId, ledSpeed);

	}

	if((startLED == 1) || (x == 1)){
		startTimer(timerLedId);
	}
}


__attribute__((__interrupt__)) void uartHandler(void) {

	int i = 0;
	int address = 0;
	int message = 0;

	usart_read_char(USART0, &i);
	//usart_putchar(USART1, i);

	startStopFlag ^= 1;

	if (!masterSlaveFlag) {
		address = (i & 0xC0) >> 6;
		start = (i & 0x20) >> 5;
		direction = (i & 0x18) >> 3;
		speed2 = i & 0x03;

		if (address == 0) {

			//usart_write_line(USART1, "--Do mnie--\n");
			if (1 == startStopFlag)
				startLED = start;
			else if (0 == startStopFlag)
				startLED = start & 0;
			directionLED = direction;
			speedLED = speed2;
			checkTimerLed();


			//message = (address << 6) | (start << 5) | (direction << 3) | speed2;

			//usart_putchar(USART1, message);

		} else if (address == 3) {

			//usart_write_line(USART1, "--Do wszystkich--\n");
			message = i;

			if (1 == startStopFlag)
				startLED = start;
			else if (0 == startStopFlag)
				startLED = start & 0;
			directionLED = direction;
			speedLED = speed2;
			checkTimerLed();

			message = (address << 6) | (start << 5) | (direction << 3) | speed2;
			usart_putchar(USART1, message);

		} else {

			//usart_write_line(USART1, "--Else--\n");
			--address;
			message = (address << 6) | (start << 5) | (direction << 3) | speed2;
			usart_putchar(USART1, message);

		}
		/*
		 if(start) usart_write_line(USART1, "start/stop ");

		 if (direction == 1) usart_write_line(USART1, "right \n");
		 else if (direction == 2) usart_write_line(USART1, "left \n");
		 else if (direction == 3) usart_write_line(USART1, "change_direction \n");

		 if (speed2 == 1) usart_write_line(USART1, "speed: up\n");
		 else if (speed2 == 2) usart_write_line(USART1, "speed: down\n");
		 */
	}

}
